using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Security;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Profile;
using FredCK.FCKeditorV2;
using System.Threading;
using System.Text.RegularExpressions;
using XZ.Content.Mailings;

namespace XZ.Web.UI.Admin
{
   public partial class SendNewsletter : BasePage
   {
      protected void Page_Load(object sender, EventArgs e)
      {
         bool isSending = false;
         Mailing.Lock.AcquireReaderLock(Timeout.Infinite);
         isSending = Mailing.IsSending;
         Mailing.Lock.ReleaseReaderLock();

         if (!this.IsPostBack && isSending)
         {
            panWait.Visible = true;
            panSend.Visible = false;
         }
         txtHtmlBody.BasePath = this.BaseUrl + "FCKeditor/";
      }

      protected void btnSend_Click(object sender, EventArgs e)
      {
          // retrieve all subscribers to the plain-text and HTML newsletter, 
          List<SubscriberInfo> subscribers = new List<SubscriberInfo>();
          ProfileCommon profile = HttpContext.Current.Profile as ProfileCommon;

          foreach (MembershipUser user in Membership.GetAllUsers())
          {
              ProfileCommon userProfile = profile.GetProfile(user.UserName);
              if (userProfile.Preferences.Newsletter != SubscriptionType.None)
              {
                 SubscriberInfo subscriber = new SubscriberInfo(
                    user.UserName, user.Email, userProfile.FirstName, userProfile.LastName,
                    userProfile.Preferences.Newsletter);
                 subscribers.Add(subscriber);
                 Mailing.Lock.AcquireWriterLock(Timeout.Infinite);
                 Mailing.TotalMails += 1;
                 Mailing.Lock.ReleaseWriterLock();
              }
          }
          Mailing.Lock.AcquireWriterLock(Timeout.Infinite);
          Mailing.Subscribers = subscribers;
          Mailing.Lock.ReleaseWriterLock();

         bool isSending = false;
         Mailing.Lock.AcquireReaderLock(Timeout.Infinite);
         isSending = Mailing.IsSending;
         Mailing.Lock.ReleaseReaderLock();
         // if another newsletter is currently being sent, show the panel with the wait message,
         // but don't hide the input controls so that the user doesn't loose the newsletter's text
         if (isSending)
         {
            panWait.Visible = true;
         }
         else
         {
            // if no newsletter is currently being sent, send this new one and
            // redirect to the page showing the progress
             Guid id = Mailing.SendMailing(txtSubject.Text,
               txtPlainTextBody.Text, txtHtmlBody.Value);
             this.Response.Redirect("SendingMailing.aspx");
         }
      }
       protected void Button1_Click(object sender, EventArgs e)
       {
           WebpageMailer message = new WebpageMailer(txtUri.Text);
           Uri uri = new Uri(txtUri.Text);
           string temp = message.HtmlBody.Replace("src=\"", "src=\"http://" + uri.Host + "/" + uri.AbsolutePath);
            temp = temp.Replace("href=\"", "href=\"http://" + uri.Host + "/" + uri.AbsolutePath);
           txtHtmlBody.Value = temp;
           txtPlainTextBody.Text = message.PlainTextBody;
       }
}
}